package com.didi.gulimall.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 10626
 */
@Configuration
public class MyMQConfig {

    @Bean
    public Queue secKillDelayQueue() {

        Map<String, Object> arguments = new HashMap<>();
        /**
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 60000
         */
        arguments.put("x-dead-letter-exchange", "seckill-event-exchange");
        arguments.put("x-dead-letter-routing-key", "seckill.rollback.order");
        arguments.put("x-message-ttl", 60000);
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue("seckill.delay.queue", true, false, false, arguments);
    }

    @Bean
    public Queue secKillReleaseOrderQueue() {
        return new Queue("seckill.release.order.queue", true, false, false);
    }

    @Bean
    public Exchange secKillEventExchange() {
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange("seckill-event-exchange", true, false);
    }


    @Bean
    public Binding secKillCreateOrderBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        return new Binding("seckill.delay.queue",
                Binding.DestinationType.QUEUE,
                "seckill-event-exchange",
                "seckill.create.order",
                null);
    }

    @Bean
    public Binding secKillReleaseAllBinding() {
        return new Binding("seckill.release.order.queue",
                Binding.DestinationType.QUEUE,
                "seckill-event-exchange",
                "seckill.rollback.order",
                null);
    }
}
